<?php

/* Load widget with widget_init function */
add_action( 'widgets_init', 'widget_contact' );

/* Register widget */
function widget_contact()
{
	register_widget( 'widget_contact' );
}

/* Handler class for all widget params */
class widget_contact extends WP_Widget
{
	/* Widget setup. */
	function widget_contact()
	{
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'widget_contact', 'description' => __('An contact widget.', 'widget_contact') );
		/* Widget control settings. */
		$control_ops = array( 'width' => 350, 'height' => 400, 'id_base' => 'widget_contact' );
		/* Create the widget. */
		$this->WP_Widget( 'widget_contact', 'Contact Widget', $widget_ops, $control_ops );
	}

	/** Widget display template */
	function widget( $args, $instance )
	{
		extract( $args );

		/* Get widget settings */
		$title = apply_filters('widget_title', $instance['title'] );

		$name    = $instance['name'];
		$phone   = $instance['phone'];
		$mail    = $instance['mail'];
		$address = $instance['address'];
		$hours   = $instance['hours'];
		$info    = $instance['info'];
		
		/* Widget themplate */
		echo $before_widget;
		
	   	if ( $title ) echo $before_title . $title . $after_title;

		echo '<div class="widget_contact_wrapper clearfix">';
		
			if ( $name != '' )    echo '<div class="contact_widget_entry clearfix"><span class="name"></span><div class="contact_widget_text">'.$name.'</div></div>';
			if ( $phone != '' )   echo '<div class="contact_widget_entry clearfix"><span class="phone"></span><div class="contact_widget_text">'.$phone.'</div></div>';
			if ( $mail != '' )    echo '<div class="contact_widget_entry clearfix"><span class="mail"></span><div class="contact_widget_text"><a class="contact_mail_link" href="mailto:'.$mail.'">'.$mail.'</a></div></div>';
			if ( $address != '' ) echo '<div class="contact_widget_entry clearfix"><span class="address"></span><div class="contact_widget_text">'.$address.'</div></div>';
			if ( $hours != '' )   echo '<div class="contact_widget_entry clearfix"><span class="hours"></span><div class="contact_widget_text">'.$hours.'</div></div>';
			if ( $info != '' )    echo '<div class="contact_widget_entry clearfix"><span class="info"></span><div class="contact_widget_text">'.$info.'</div></div>';

		echo '</div>';
		echo $after_widget;
	}

	/* Update widget settings. */
	function update( $new_instance, $old_instance )
	{
		$instance = $old_instance;

		/* Create widget settings instances. */
		$instance['title'] = strip_tags( $new_instance['title'] );
		
		$instance['name']    = $new_instance['name'];
		$instance['phone']   = $new_instance['phone'];
		$instance['mail']    = $new_instance['mail'];
		$instance['address'] = $new_instance['address'];
		$instance['hours']   = $new_instance['hours'];
		$instance['info']    = $new_instance['info'];
		return $instance;
	}

	/* Admin panel form  */
	function form( $instance )
	{
		$instance = wp_parse_args( (array) $instance );

		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('title'); echo '">'; echo 'Widget Title'; echo '</label>';
			echo '<br />';
			echo '<input id="'; echo $this->get_field_id('title'); echo '" name="'; echo $this->get_field_name('title'); echo '" value="'; if( isset($instance['title']) ) echo $instance['title']; echo '" />';
		echo '</p>';
		
		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('name'); echo '">'; echo 'Name:'; echo '</label>';
			echo '<br />';
			echo '<input id="'; echo $this->get_field_id('name'); echo '" name="'; echo $this->get_field_name('name'); echo '" value="'; if( isset($instance['name']) ) echo $instance['name']; echo '" />';
		echo '</p>';
		
		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('phone'); echo '">'; echo 'Phone:'; echo '</label>';
			echo '<br />';
			echo '<input id="'; echo $this->get_field_id('phone'); echo '" name="'; echo $this->get_field_name('phone'); echo '" value="'; if( isset($instance['phone']) ) echo $instance['phone']; echo '" />';
		echo '</p>';
		
		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('mail'); echo '">'; echo 'Email:'; echo '</label>';
			echo '<br />';
			echo '<input id="'; echo $this->get_field_id('mail'); echo '" name="'; echo $this->get_field_name('mail'); echo '" value="'; if( isset($instance['mail']) ) echo $instance['mail']; echo '" />';
		echo '</p>';
		
		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('address'); echo '">'; echo 'Address:'; echo '</label>';
			echo '<br />';
			echo '<input id="'; echo $this->get_field_id('address'); echo '" name="'; echo $this->get_field_name('address'); echo '" value="'; if( isset($instance['address']) ) echo $instance['address']; echo '" />';
		echo '</p>';
		
		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('hours'); echo '">'; echo 'Working Hours:'; echo '</label>';
			echo '<br />';
			echo '<textarea id="'; echo $this->get_field_id('hours'); echo '" name="'; echo $this->get_field_name('hours'); echo '">'; if( isset($instance['hours']) ) echo $instance['hours']; echo '</textarea>';
		echo '</p>';
		
		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('info'); echo '">'; echo 'Extra info:'; echo '</label>';
			echo '<br />';
			echo '<textarea id="'; echo $this->get_field_id('info'); echo '" name="'; echo $this->get_field_name('info'); echo '">'; if( isset($instance['info']) ) echo $instance['info']; echo '</textarea>';
		echo '</p>';
		
	}
}
///////////// CONTACT WIDGET END //////////
?>